<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='轮播管理',active='carousel'">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">轮播管理</h4>
                    </div>
                    <div class="col-md-12 layui-form">
                        <div class="pull-right">
                            <a th:href="@{/admin/carousel/addCarousel}"
                               class="btn btn-success waves-effect waves-light m-b-5">添加轮播</a>
                        </div>
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th width="5%">序号</th>
                                <th width="20%">轮播标题</th>
                                <th width="25%">图片</th>
                                <th width="15%">图片链接地址</th>
                                <th width="7%">排序值</th>
                                <th width="10%">轮播图状态</th>
                                <th width="20%">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="post : ${carousels.list}">
                                <tr th:cid="${post.id}">
                                    <td>
                                        <span th:text="${post.id}"></span>
                                    </td>
                                    <td>
                                        <span th:text="${post.title}"></span>
                                    </td>
                                    <td height="200" width="325">
                                        <div style="width: 325px;height: 200px;">
                                            <img style="width: 100%;height:100%;"
                                                 th:src="${post.url}" alt=""/>
                                        </div>
                                    </td>
                                    <td>
                                        <th:block th:text="${post.url}"/>
                                    </td>
                                    <td>
                                        <th:block th:text="${post.rank}"/>
                                    </td>
                                    <td style="text-align: center;">
                                        <input type="checkbox" name="state" lay-skin="switch"
                                               lay-text="启用|禁用" th:value="${post.flag}"
                                               th:checked="${post.flag} eq 'y'?'true':'false'"/>
                                        <input type="hidden" id="cid" th:value="${post.id }"/>
                                    </td>
                                    <td>
                                        <a th:href="@{'/admin/article/'+${post.id}}"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-edit"></i> <span>编辑</span></a>
                                        <a href="javascript:void(0)" th:onclick="'delPost('+${post.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-trash-o"></i> <span>删除</span></a>
                                    </td>
                                </tr>
                            </th:block>
                            </tbody>
                        </table>
                        <div th:replace="comm/macros :: pageAdminNav(${carousels})"></div>
                    </div>
                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="admin/footer :: footer"></div>
<script th:src="@{https://www.layuicdn.com/layui-v2.5.4/layui.js}"></script>
<script type="text/javascript">
    /*<![CDATA[*/
    var tale = new $.tale();
    function delPost(cid) {
        tale.alertConfirm({
            title: '确定删除该轮播吗?',
            then: function () {
                tale.post({
                    url: '/admin/carousel/delete',
                    data: {cid: cid},
                    success: function (result) {
                        console.log(result.code);
                        if (result.code == 0) {
                            tale.alertOkAndReload('轮播删除成功');
                        } else {
                            tale.alertError(result.msg || '轮播删除失败');
                        }
                    }
                });
            }
        });
    }


    /*]]>*/
    layui.use(['form'], function () {
        var form = layui.form;
        form.on('switch', function (data) {
            // debugger;
            console.log(data.elem.checked); //开关是否开启，true或者false
            var id = $(data.othis).next("input").val();
            var currentpre = $(data.othis).prev();
            console.log(currentpre);
            var flag;
            if (data.elem.checked == true) {
                $.ajax({
                    type: "post",
                    async: false,
                    data: {
                        "id": id,
                        "flag": 1
                    },
                    url: "/admin/carousel/changeCarouselState",
                    success: function (data) {
                        flag = data;
                    }
                });
                if (flag == "over") {
                    currentpre.prop("checked", false);
                    layer.msg("启用轮播状态数不能超过4个!");
                    form.render();
                } else {
                    layer.msg("已启用!");
                }
            } else {
                $.ajax({
                    type: "post",
                    async: false,
                    data: {
                        "id": id,
                        "flag": 0
                    },
                    url: "/admin/carousel/changeCarouselState",
                    success: function (data) {
                        flag = data;
                    }
                });
                if (flag == "yes") {
                    layer.msg("已禁用!");
                }
            }
        });
    });

</script>
</body>
</html>